package com.mk.dao.config;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 根据ID强制物理删除 无视逻辑删除标记
 *
 * @author fuc
 * @since 2022/03/30
 */
public class HardDeleteByIds extends AbstractMethod {

    /**
     * @param methodName 方法名
     * @since 3.5.0
     */
    protected HardDeleteByIds(String methodName) {
        super(methodName);
    }

    @Override
    public MappedStatement injectMappedStatement(
            Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
        sql =
                String.format(
                        sqlMethod.getSql(),
                        tableInfo.getTableName(),
                        tableInfo.getKeyColumn(),
                        SqlScriptUtils.convertForeach(
                                SqlScriptUtils.convertChoose(
                                        "@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
                                        "#{item}",
                                        "#{item." + tableInfo.getKeyProperty() + "}"),
                                COLL,
                                null,
                                "item",
                                COMMA));
        SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
        return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
    }
}
